Show language: C# VB.NET Both
By directly hooking into the Configuration.CentralEventDispatcher.Action event we can easily obtain the preliminary results of a search, which we can then filter, reorder etc.
The code below handles the "Action" event in the central event system. It processes "ResultItemsFinalized" notifications, and uses the event argument to access the preliminary results.
Eg. to filter any results that include the word "News" in the title, the following code is used in the code-behind of the form holding the SearchResult control.
protected void Page_Load(object sender, EventArgs e)
{
SearchResult1.FilterLoadLevel = Keyoti.SearchEngine.Search.FilterLoadLevel.Everything;
SearchResult1.Configuration.CentralEventDispatcher.Action += new
Keyoti.SearchEngine.Events.ActionEventHandler(CentralEventDispatcher_Action);
}
void CentralEventDispatcher_Action(object sender, Keyoti.SearchEngine.Events.ActionEventArgs e)
{
if (e.ActionData.Name == Keyoti.SearchEngine.Events.ActionName.ResultItemsFinalized)
{
Keyoti.SearchEngine.Utils.ResultItemList results =
e.ActionData.Data as Keyoti.SearchEngine.Utils.ResultItemList;
for (int i = 0; i < results.Count; i++)
{
if (results[i].Title.Contains("News"))
{
results.RemoveAt(i);
i--;//need to rerun over this element number.
}
}
}
}
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
SearchResult1.FilterLoadLevel = Keyoti.SearchEngine.Search.FilterLoadLevel.Everything
AddHandler SearchResult1.Configuration.CentralEventDispatcher.Action, _
AddressOf Me.CentralEventDispatcher_Action
End Sub
Private Sub CentralEventDispatcher_Action(ByVal sender As Object, _
ByVal e As Keyoti.SearchEngine.Events.ActionEventArgs)
If (e.ActionData.Name = Keyoti.SearchEngine.Events.ActionName.ResultItemsFinalized) Then
Dim results As Keyoti.SearchEngine.Utils.ResultItemList = _
CType(e.ActionData.Data,Keyoti.SearchEngine.Utils.ResultItemList)
Dim i As Integer = 0
Do While (i < results.Count)
If results(i).Title.Contains("News") Then
results.RemoveAt(i)
i = (i - 1)
'need to rerun over this element number.
End If
i = (i + 1)
Loop
End If
End Sub
Eg. to filter any results that include the word "News" in the title, the following code is used in the class that uses SearchAgent
{
...
SearchAgent agent = new SearchAgent(....);
agent.FilterLoadLevel = Keyoti.SearchEngine.Search.FilterLoadLevel.Everything;
agent.Configuration.CentralEventDispatcher.Action += new
Keyoti.SearchEngine.Events.ActionEventHandler(CentralEventDispatcher_Action);
...
agent.Search(....);
}
void CentralEventDispatcher_Action(object sender, Keyoti.SearchEngine.Events.ActionEventArgs e)
{
...same as the example above for the SearchResult control...
}
Sub ...
...
Dim agent As New SearchAgent(....)
agent.FilterLoadLevel = Keyoti.SearchEngine.Search.FilterLoadLevel.Everything
AddHandler agent.Configuration.CentralEventDispatcher.Action, _
AddressOf Me.CentralEventDispatcher_Action
End Sub
Private Sub CentralEventDispatcher_Action(ByVal sender As Object, _
ByVal e As Keyoti.SearchEngine.Events.ActionEventArgs)
...same as the example above for the SearchResult control...
End Sub
SearchResult1.FilterLoadLevel = Keyoti.SearchEngine.Search.FilterLoadLevel.Everything
The FilterLoadLevel property specifies what amount of data will be loaded for the ResultItemsFinalized stage. It is important to note that the default setting of DocId is the fastest, since it minimizes what may be redundant data loading (assuming that most results are never seen by a user). If the Title was not used, but instead the Uri, or category properties, then the setting of DocIdUrlCategories could be used instead of Everything (which is more optimal).
The above code filters on the Title, but the Uri, or other properties can easily be used, for example the condition
if (results[i].UriString.ToLower().EndsWith(".pdf"))
filters PDF files.